package com.dianqiu.common.dao.impl;


import java.util.List;

import org.springframework.stereotype.Component;
import com.dianqiu.base.dao.impl.BaseDao;
import com.dianqiu.common.bean.DqHandicapTO;
import com.dianqiu.common.dao.DqHandicapDao;


@Component("dqHandicapDao")
public class DqHandicapDaoImpl extends BaseDao implements DqHandicapDao {

	public DqHandicapTO getHandicapByMatchId(Integer matchId) {
		DqHandicapTO dqHandicapTO = null;
		List<DqHandicapTO> handicapList = this.getHibernateTemplate().find(" from DqHandicapTO handicap " +
																		   " left outer join fetch handicap.dqHandicapType handicapType" +
																		   " left outer join fetch handicap.dqMatch match " +
																		   " where match.mthId = " + matchId + 
																		   " order by handicap.updatedDt ");
		if(handicapList!=null && handicapList.size()>0){
			dqHandicapTO = handicapList.get(0);
		}
		return dqHandicapTO;
	}
	

	@Override
	public DqHandicapTO getHandicapByBetCompanyIdAndMatchId(Integer companyId,
			Integer matchId,String order) {
		DqHandicapTO dqHandicapTO = null;
		List<DqHandicapTO> handicapList = this.getHibernateTemplate().find(" from DqHandicapTO handicap " +
																		   " left outer join fetch handicap.dqHandicapType handicapType" +
																		   " left outer join fetch handicap.dqMatch match " +
																		   " left outer join fetch handicap.dqBetCompany betCompany " +
																		   " where match.mthId = " + matchId + 
																		   " and  betCompany.cmpyId = " +companyId +
																		   "order by handicap.updatedDt " + order );
		if(handicapList!=null && handicapList.size()>0){
			dqHandicapTO = handicapList.get(0);
		}
		return dqHandicapTO;
	}
	
	public DqHandicapTO getHandicapById(Integer id) {
		DqHandicapTO dqHandicapTO = null;
		List<DqHandicapTO> handicapList = this.getHibernateTemplate().find(" from DqHandicapTO handicap " +
																		   " left outer join fetch handicap.dqBetCompany betCompany " +
																		   " left outer join fetch handicap.dqHandicapType " +
																		   " left outer join fetch handicap.dqMatch " +
																		   " where handicap.hdpId = " + id );
		if(handicapList!=null && handicapList.size()>0){
			dqHandicapTO = handicapList.get(0);
		}
		return dqHandicapTO;
	}
	
	
	public List<DqHandicapTO> getHandicapListByMatchId(Integer matchId) {
		DqHandicapTO dqHandicapTO = null;
		List<DqHandicapTO> handicapList = this.getHibernateTemplate().find(" from DqHandicapTO handicap " +
																		   " left outer join fetch handicap.dqBetCompany betCompany " +
																		   " left outer join fetch handicap.dqHandicapType handicapType" +
																		   " left outer join fetch handicap.dqMatch match " +
																		   " left outer join fetch match.dqClubsByHomeClbId homeClb " +
																		   " left outer join fetch match.dqClubsByAwayClbId awayClb " +
																		   " where match.mthId = " + matchId + 
																		   " order by handicap.updatedDt ");
		
		return handicapList;
	}
	
	public void saveDqHandicap(DqHandicapTO dqHandicap){
		this.getHibernateTemplate().save(dqHandicap);
	}
	
}



